// Dynare code for ARGEMminTrilema_TaxOnLEVEL 
// Guillermo Escud�, March 2014.
// To be used with Dynare Version 4.2.0

// A DSGE model for a SOE that allows for systematic intervention in FX market
// using 1, 2 or 3 feedback rules and hence 7 general regimes:
// MER+CC: Managed Exchange Rate with Capital Control regime:  3 feedback rules
// FER+CC: Floating Exchange Rate with Capital Control regime:  2 feedback rules
// PER+CC: Pegged Exchange Rate with Capital Control regime:  2 feedback rules
// MER: Managed Exchange Rate regime:  2 feedback rules
// FER: Floating Exchange Rate regime: 1 feedback rule (Taylor rule)
// PER Pegged Exchange Rate regime:    1 (possibly feedback) rule for the rate of nominal depreciation
// CC Capital Control regime: 1 (possibly feedback) rule for tax on D

// FOR SIMPLE POLICY RULES OR OPTIMAL SIMPLE RULES (osr):

// For the MER+CC regime:
// Use the 3 simple policy rules

// For the FER+CC regime:
// Replace the second policy rule by equation 'r=r_ss' 

// For the PER+CC regime:
// Replace the first policy rule by equation 'b=b_ss'

// For the MER regime:
// Replace the third policy rule by equation 'taxD=taxD_ss;'

// For the FER regime:
// Replace the second policy rule by equation 'r=r_ss' 
// and the third policy rule by equation 'taxD=taxD_ss;'

// For the PER regime:
// Replace the first policy rule by equation 'b=b_ss'
// and the third policy rule by equation 'taxD=taxD_ss;'
 
// For the CC regime:
// Replace the first policy rule by equation 'b=b_ss'
// and the second policy rule by equation 'r=r_ss' 

// FOR OPTIMAL RULES UNDER COMMITMENT (ramsey_policy):

// Comment out the 3 policy rules
// Comment out 'steady', 'check', 'resid', and 'stoch_simul' 
// Use 'planner_objective' and 'ramsey_policy'
// For MER+CC regime use option 'instruments=(ii,delta,taxD)'
// For FER+CC regime use option 'instruments=(ii,taxD)' and the equation 'r=r_ss' 
// For PER+CC regime use option 'instruments=(delta,taxD)' and the equation 'b=b_ss'
// For MER regime use option 'instruments=(ii,delta)' and the equation 'taxD=taxD_ss'
// For FER regime use option 'instruments=(ii)' and the equations 'r=r_ss','taxD=taxD_ss' 
// For PER regime use option 'instruments=(delta)' and the equations 'b=b_ss','taxD=taxD_ss'
// For CC regime use option 'instruments=(taxD_ss)' and the equations 'b=b_ss','r=r_ss'

close all;

// ENDOGENOUS VARIABLES

var ii delta r b e piC Y d  C pii Q mc Deltta pC pStar varphiM tauM 
m X TB w z_piStar z_piStarX z_G z_epsilon z_iStar z_phiStar GammaP PsiP 
real_ii tax qf CA N dii ddelta gammaD gammaM gammaR varphiD tauD 
taxDCol taxD  ;

// EXOGENOUS VARIABLES

varexo eps_epsilon eps_G eps_iStar eps_phiStar eps_piStar eps_piStarX ; 


// PARAMETERS

parameters 

// Monetary and XR simple policy rules
h_0 h_1 h_2 h_3               //Interest rate Simple Rule 
k_0 k_1 k_2 k_3 k_4           //Exchange rate Simple Rule 
j_0 j_1 j_2 j_3 j_4           //taxD Simple Rule 


//Weights for ramsey ad-hoc CB loss function
omega_piC omega_Y omega_e omega_r omega_dii omega_ddelta

omega_taxD

// Persistence parameters in shock variables          
rhoPiStar rhoPiStarX rhoG rhoEpsilon rhoiStar rhoPhiStar rhoPiStarNX

// Great ratios  
gammaR_ss gammaD_ss gammaM_ss pCCtoy mtoy btoy GtopCC tbetoy 

// General                                                                           
piT betta sigmaC a_D thetta sigmaN alfa  bA bX kappaX 
varepsilon_L ELASTtauBarD_1 ELASTtauBarD_2
alpha_1 alpha_2 alpha_3 
xiN betta_1 betta_2 betta_3 thettaC
EC_speed_N EC_speed_X 

// Steady state values of endogenous variables
Y_ss piStar_ss iStar_ss phiStar_ss pStar_ss  e_ss C_ss d_ss r_ss
m_ss b_ss mc_ss Q_ss GammaP_ss PsiP_ss N_ss pC_ss 
X_ss TB_ss Deltta_ss w_ss varphiM_ss tauM_ss tax_ss qf_ss taxD_ss
tauD_ss varphiD_ss taxDCol_ss ;

// PARAMETER VALUES

// Monetary and XR simple policy rules (these don't affect the SS)

h_0=1.3; h_1=2.1; h_2=-0.01;   h_3=0.05;               
k_0=-0.2; k_1=-0.4; k_2=0.1;  k_3=-0.3;  k_4=-0.1 ;   
j_0=0.5; j_1=-0.2; j_2=0.0;   j_3=0.0;  j_4=-0.03 ;

// Calibrated steady state values of endogenous variables

Y_ss=1.443;  piStar_ss=1; iStar_ss=1.03^0.25; phiStar_ss=1.005^0.25; pStar_ss=1;

// Persistence parameters in shock variables (these don't affect the SS)

rhoEpsilon=0.8; rhoG=0.85; rhoiStar=0.7; rhoPhiStar=0.3;
rhoPiStar=0.20; rhoPiStarX= 0.41 ; rhoPiStarNX=0.18;

betta=0.99;            //Intertemporal discount factor                    

taxD_ss=0.1*(1-betta*phiStar_ss*iStar_ss/piStar_ss);

piT=1.015;             //Inflation target        
alfa=0.66;             //Probability of non-optimizing price
sigmaC=1.5;            //Constant relative risk aversion for consumption
sigmaN=0.5 ;           //Constant relative risk aversion for labor CAMBI� DESDE 0.5 EN ARGEMmin PARA QUE PROD SHOCK HAGA SUBIR N EN LUGAR DE BAJAR
a_D=0.86;              //Home bias in consumption
thetta=6;              //Elasticity of substitution for domestic goods
thettaC=1.5;           //Elasticity of substitution between domestic and imported bundles
bA=0.5;                //Exogenous parameter for exports demand
varepsilon_L=1.02;     //Elasticity of money/consumption function w.r. to ii
gammaD_ss=0.5;         //Household Foreign Debt to GDP ratio 

ELASTtauBarD_1=10;     //Debt/GDP ratio elasticity of tauBarD 
ELASTtauBarD_2=0;      //Reserves/GDP ratio elasticity of tauBarD (in UIP)

EC_speed_N=0.181;      //Imports error correction speed 
EC_speed_X=-0.255;     //Exports error correction speed 
bX=1/(1-bA);           //Composite parameter for exports demand
kappaX=bA^(bA/(1-bA)); //Composite parameter for exports demand

// Risk premium parameters 
gammaR_ss=0.13;         //CB reserves to GDP ratio
alpha_1=((1-taxD_ss)*(1/(betta*phiStar_ss*iStar_ss/piStar_ss))-1)/((1+ELASTtauBarD_1)*(1+ELASTtauBarD_1+ELASTtauBarD_2));
alpha_2=(ELASTtauBarD_1/(1+ELASTtauBarD_1+ELASTtauBarD_2))/gammaD_ss;
alpha_3=((-ELASTtauBarD_2)/(1+ELASTtauBarD_1+ELASTtauBarD_2))/gammaR_ss;
tauD_ss=1+alpha_1/(1-alpha_2*gammaD_ss+alpha_3*gammaR_ss);                            
varphiD_ss=1+(tauD_ss-1)*(1+alpha_2*gammaD_ss/(1-alpha_2*gammaD_ss+alpha_3*gammaR_ss));

// Great ratios

tbetoy=((iStar_ss/piStar_ss)*phiStar_ss*tauD_ss-1)*gammaD_ss-(iStar_ss/piStar_ss-1)*gammaR_ss;
f_e = @(e_ss) ((kappaX*(e_ss*pStar_ss)^bX)*(a_D+(1-a_D)*(e_ss)^(1-thettaC))-(1-a_D)*e_ss^(1-thettaC))/a_D-tbetoy;
e_ss=fzero(f_e,0.59506);                       
TB_ss=tbetoy*Y_ss/e_ss;
X_ss=Y_ss*kappaX*(e_ss*pStar_ss)^bX;
Q_ss=Y_ss-(1-bA)*X_ss;
Deltta_ss=((1-alfa)/((1-alfa*piT^(thetta))))*((1-alfa*piT^(thetta-1))/(1-alfa))^(thetta/(thetta-1));
N_ss=Q_ss*Deltta_ss;

// Transactions cost parameters 

gammaM_ss=0.095522;     //money/consumption ratio

GtopCC=1.19;            //Gov expenditure to private consumption ratio
betta_3=160;            //Should be >1/(varepsilon_L*(piT/betta-1))-1 
betta_2=(1/gammaM_ss)*(1/(((betta_3+1)*varepsilon_L*(piT/betta-1))-1));                        
betta_1=(1-betta/piT)*((1+betta_2*gammaM_ss)^(betta_3+1))/(betta_2*betta_3);          
tauM_ss=1+betta_1/(1+betta_2*gammaM_ss)^betta_3;
varphiM_ss=1+(tauM_ss-1)*(1+betta_3*((betta_2*gammaM_ss)/(1+betta_2*gammaM_ss))); 

pC_ss=(a_D+(1-a_D)*(e_ss)^(1-thettaC))^(1/(1-thettaC));    
pCCtoy=(1-kappaX*(e_ss*pStar_ss)^bX)*(pC_ss^(1-thettaC)/(a_D*GtopCC*tauM_ss));
C_ss=pCCtoy*Y_ss/pC_ss;         
mtoy=gammaM_ss*pCCtoy;     //money/GDP ratio
btoy=gammaR_ss-mtoy;       //bonds/GDP ratio

// Steady state values of endogenous variables
  
GammaP_ss=((Q_ss/(pC_ss*C_ss^sigmaC))/(1-betta*alfa*piT^(thetta-1)));
PsiP_ss=GammaP_ss/(((1-alfa*piT^(thetta-1))/(1-alfa))^(1/(thetta-1)));
mc_ss=PsiP_ss*(1-betta*alfa*((piT)^thetta))/((thetta/(thetta-1))*(Q_ss/(pC_ss*C_ss^sigmaC)));    
w_ss=mc_ss;
r_ss=gammaR_ss*Y_ss/e_ss;
b_ss=btoy*Y_ss;
qf_ss=((iStar_ss)-1/(piT/piStar_ss))*(e_ss*r_ss/piStar_ss)-(piT/betta-1)*(b_ss/piT);
d_ss=gammaD_ss*Y_ss/e_ss;
m_ss=mtoy*Y_ss;
xiN=w_ss/(N_ss^sigmaN*pC_ss*C_ss^sigmaC*(varphiM_ss));
taxDCol_ss=taxD_ss*e_ss*d_ss;
tax_ss=(GtopCC-1)*tauM_ss*pC_ss*C_ss-qf_ss-taxDCol_ss;

//For ad-hoc Ramsey:

omega_piC=1; omega_Y=100; omega_e=1; omega_r=1; omega_dii=50; omega_ddelta=50  ;

omega_taxD=1;  //NEED TO RAISE GOV BETA TO 0.999 IF omega_taxD=0 FOR BK


// MODEL EQUATIONS

model;

// FOR SIMPLE POLICY RULES OR OPTIMAL SIMPLE RULES (osr):

// For the MER+CC regime:
// Use the 3 simple policy rules

// For the FER+CC regime:
// Replace the second policy rule by equation 'r=r_ss' 

// For the PER+CC regime:
// Replace the first policy rule by equation 'b=b_ss'

// For the MER, FER and PER regimes (without CC):
// Replace the third policy rule by equation 'taxD=taxD_ss' 

// For the MER regime(without CC):
// Use the first two simple policy rules 

// For the FER regime (without CC):
// Replace the second policy rule by equation 'r=r_ss' 

// For the PER regime (without CC):
// Replace the first policy rule by equation 'b=b_ss'

// For the CC regime :
// Replace the first policy rule by equation 'b=b_ss'
// Replace the second policy rule by equation 'r=r_ss' 

// FOR OPTIMAL POLICY UNDER COMMITMENT (ramsey_policy):

// Comment out 'steady', 'check', 'resid', and 'stoch_simul' 
// Comment out the 3 simple feedback policy rules
// Use 'planner_objective' and 'ramsey_policy'

// For MER+CC regime use option 'instruments=(ii,delta,taxD)' and comment out the 3 equations that fix the endogenous variables to NSS value
// For FER+CC regime use option 'instruments=(ii,taxD)' and the equation 'r=r_ss' 
// For PER+CC regime use option 'instruments=(delta,taxD)' and the equation 'b=b_ss'
// For MER regime use option 'instruments=(ii,delta) and the equation 'taxD=taxD_ss'
// For FER regime use option 'instruments=(ii)' and the equations 'r=r_ss' and 'taxD=taxD_ss' 
// For PER regime use option 'instruments=(delta)' and the equations 'b=b_ss' and 'taxD=taxD_ss'
// For CC regime use option 'instruments=(taxD)' and the equations 'r=r_ss' and 'b=b_ss'


// Simple policy rules (comment out all 3 for 'ramsey' in MER regime)

// 1 Taylor rule

ii=(piT/betta)^(1-h_0)*(ii(-1))^h_0*(piC/piT)^h_1*(Y/Y_ss)^h_2*(e/e_ss)^h_3;

//For Pegged Exchange Rate (PER) policy use instead:
//b=b_ss;

// 2 Exchange rate depreciation rule:

delta=(piT/piStar_ss)^(1-k_0)*delta(-1)^k_0*(piC/piT)^k_1*(Y/Y_ss)^k_2*(e/e_ss)^k_3*(gammaR/gammaR_ss)^k_4;

//For Floating Exchange Rate (FER) policy use instead:
//r=r_ss;


// 3 Tax on capital flows rule:

taxD=taxD_ss^(1-j_0)*taxD(-1)^j_0*(piC/piT)^j_1*(Y/Y_ss)^j_2*(e/e_ss)^j_3*(z_phiStar/log(phiStar_ss))^j_4;

//taxD=taxD_ss;


// 4 Central Bank balance sheet

b=e*r-m;

// 5 Money market clearing

m=(1/betta_2)*(((betta_1*betta_2*betta_3/(1-1/ii))^(1/(betta_3+1)))-1)*pC*C;    

// 6 Trade Balance (in foreign currency)

TB=(1/(a_D*e))*(pC^(1-thettaC)*X-(1-a_D)*e^(1-thettaC)*Y);

// 7 8 Current account (in foreign currency)

tauD=1+alpha_1/(1-alpha_2*gammaD+alpha_3*gammaR);

CA=(exp(z_iStar(-1))/exp(z_piStar)-1)*r(-1)-((exp(z_iStar(-1))/exp(z_piStar))*exp(z_phiStar(-1))*(tauD(-1))-1)*d(-1)+TB;

// 9 Balance of Payments  (in foreign currency)

r=d+CA+r(-1)-d(-1);

// 10 Exports (in foreign currency)

X=Y*kappaX*((e*pStar)^bX);

// 11 RER identity

e=e(-1)*(delta*exp(z_piStar)/pii);

// 12 13 Risk-adjusted uncovered interest parity 

varphiD=1+(tauD-1)*(1+alpha_2*gammaD/(1-alpha_2*gammaD+alpha_3*gammaR));

ii=exp(z_iStar)*exp(z_phiStar)*(varphiD/(1-taxD))*delta(+1); 

// 14 Labor market clearing

w=xiN*N^sigmaN*pC*C^sigmaC*varphiM;

// 15 Hours worked

N=Q*Deltta/exp(z_epsilon);

// 16 Domestic goods market clearing

Q=Y-(1-bA)*X;

// 17 GDP DEFINITION

Y=a_D*exp(z_G)*tauM*pC^thettaC*C+X;         

// 18 Dynamics of Price dispersion

Deltta=alfa*pii^thetta*Deltta(-1)+(1-alfa)*((1-alfa*pii^(thetta-1))/(1-alfa))^(thetta/(thetta-1));

// 19-21 Domestic inflation Phillips equation 

GammaP=PsiP*((1-alfa*pii^(thetta-1))/(1-alfa))^(1/(thetta-1));

GammaP=Q/(pC*C^sigmaC)+betta*alfa*(pii(+1))^(thetta-1)*GammaP(+1);       

PsiP=(thetta/(thetta-1))*(Q/(pC*C^sigmaC))*mc+betta*alfa*((pii(+1))^thetta)*PsiP(+1);    

// 22 Real Marginal Cost

mc=w/exp(z_epsilon);

// 23 Transactions cost (net)

tauM=1+betta_1/(1+betta_2*gammaM)^betta_3;

// 24 Transactions cost effect on the Euler equation (gross)

varphiM=1+(tauM-1)*(1+betta_3*((betta_2*gammaM)/(1+betta_2*gammaM))); 

// 25 Consumption relative price

pC^(1-thettaC)=a_D+(1-a_D)*e^(1-thettaC);   

// 26 Consumption inflation identity

piC=pii*pC/pC(-1);    

// 27 Euler equation

C^(-sigmaC)/varphiM=betta*ii*(C(+1)^(-sigmaC)/piC(+1))/varphiM(+1);

// 28 External Terms of Trade

pStar=pStar(-1)*exp(z_piStarX)/exp(z_piStar);     


// VARIABLES SUBJECT TO STOCHASTIC SHOCKS BLOCK (Shocked variables are in logs)

// 29 30 Cointegrated RW prices

z_piStarX=z_piStarX(-1)*rhoPiStarX+log(pStar(-1))*EC_speed_X+eps_piStarX;

z_piStar=z_piStar(-1)*rhoPiStar+log(pStar(-1))*EC_speed_N+z_piStar(-1)*rhoPiStarNX+eps_piStar;

// 31 Government expenditure shock 

z_G=(z_G(-1))*rhoG+log(GtopCC)*(1-rhoG)+eps_G;

// 32 Transitory productivity shock 

z_epsilon=z_epsilon(-1)*rhoEpsilon+eps_epsilon;

// 33 RW riskfree interest rate shock 

z_iStar=(z_iStar(-1))*rhoiStar+log(iStar_ss)*(1-rhoiStar)-eps_iStar; //EST� PARA SHOCK NEGATIVO. baja de z_iStar 

// 34 RW Financing liquidity shock 

z_phiStar=z_phiStar(-1)*rhoPhiStar+log(phiStar_ss)*(1-rhoPhiStar)-eps_phiStar; //EST� PARA SHOCK NEGATIVO. ENTRADA DE K

// 35 Real interest rate

real_ii=ii/piC(+1);

// 36 Quasi-fiscal surplus

qf=(exp(z_iStar(-1))-1/delta)*e*r(-1)/exp(z_piStar)-(ii(-1)-1)*b(-1)/pii;

// 37 38 Tax collection

taxDCol=taxD*e*d;

tax=(exp(z_G)-1)*tauM*pC*C-qf-taxDCol;

// 39 40 Policy instruments differences for ramsey

dii=ii-ii(-1);

ddelta=delta-delta(-1);

// 41-43 Great ratios

gammaD=d*e/Y;

gammaM=m/(pC*C);

gammaR=e*r/Y;

end;

//steady and check can�t be used for ramsey:
//steady;  
//check;

shocks;

var eps_epsilon;        stderr  0.01 ;   
var eps_G;              stderr  0.03 ;  
var eps_iStar;          stderr  0.0046 ;    
var eps_phiStar;        stderr  0.05 ; 
var eps_piStar;         stderr  0.0295 ;   
var eps_piStarX;        stderr  0.0424 ;   

end;


steady_state_model;

ii=piT/betta; delta=piT/piStar_ss;  taxD=taxD_ss; 
r=r_ss; b=b_ss; e=e_ss; piC=piT; Y=Y_ss; 
d=d_ss;  C=C_ss; pii=piT; Q=Q_ss;  mc=mc_ss;  Deltta=Deltta_ss; pC=pC_ss; 
pStar=pStar_ss; varphiM=varphiM_ss; tauM=tauM_ss;  m=m_ss; X=X_ss; TB=TB_ss; w=w_ss; 
z_piStar=log(piStar_ss);  z_piStarX=log(piStar_ss);  z_G=log(GtopCC); z_epsilon=log(1); 
z_iStar=log(iStar_ss); z_phiStar=log(phiStar_ss); GammaP=GammaP_ss; PsiP=PsiP_ss;
real_ii=ii/piC; tax=tax_ss; qf=qf_ss; CA=0; N=N_ss; dii=0; ddelta=0; 
gammaD=gammaD_ss; gammaM=gammaM_ss; gammaR=gammaR_ss;  
varphiD=varphiD_ss; tauD=tauD_ss; taxDCol=taxDCol_ss; 

end;

stoch_simul(order=1,irf=0,solve_algo=2)  ;


// FOR OPTIMAL SIMPLE RULES:

// Provides weights for objective function:
//optim_weights;
//piC 100; Y 1 ; e 1; r 0; dii 50; ddelta 50;
//end;

// Specifies parameters to be optimized:
//osr_params h_0 h_1 h_2 h_3    k_0 k_1 k_2 k_3 k_4  j_0 j_1 j_2  j_3  j_4  ;
// 

//Provides numerical initialization for the optimization:

//MER+CC

//h_0=0.2; h_1=1.2;   h_2=0;    h_3=0;	
//k_0=0.1; k_1=0;	  k_2=-0.2;    k_3=-0.22; k_4=-0.01;
//j_0=-0.1;	j_1=-0.2;	j_2=0.0;	j_3=0.0;	j_4=0.1;

// FER+CC

//h_0=0.2; h_1=1.2; h_2=0;   h_3=0;               
//j_0=0.0; j_1=-0.2; j_2=0.0;   j_3=0.0;  j_4=-0.02;

// PER+CC

//k_0=0.4; k_1=0; k_2=0;  k_3=0;    k_4=-0.02 ;   
//j_0=0.1; j_1=-0.2; j_2=0.0;   j_3=0.0;  j_4=-0.01; 

// MER

//h_0=0.71; h_1=1.4; h_2=-0.0;   h_3=0.2;               
//k_0=0.4; k_1=-0.1; k_2=-1;  k_3=0.3;    k_4=-0.2 ; 

// FER

//h_0=0.6; h_1=1.2; h_2=0.0;   h_3=0;               

// PER

//k_0=-0.9; k_1=-1.2; k_2=-0.5;  k_3=1;    k_4=-0.02 ; 

// CC

//j_0=2.2; j_1=-1; j_2=-1;   j_3=1;  j_4=-0.05;

//Instructs for osr
//osr(order=1,irf=0,solve_algo=2) piC Y e ii delta gammaD taxD tax taxDCol ;



// FOR OPTIMAL POLICY under commitment:

//planner_objective((omega_piC*(piC-piT)^2+omega_Y*(Y-Y_ss)^2+omega_e*(e-e_ss)^2+omega_r*(r-r_ss)^2+omega_dii*dii^2+omega_ddelta*ddelta^2+omega_taxD*(taxD-taxD_ss)^2));

//FOR MER+CC
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii,delta,taxD),irf=0,solve_algo=2) piC Y e ii	delta d gammaD  taxD taxDCol;

//FOR FER+CC
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii,taxD),irf=0,solve_algo=2)e gammaD ii	delta	taxD piC Y 	r b  ;

//FOR PER+CC
//ramsey_policy(planner_discount=0.99,order=1,instruments=(delta,taxD),irf=0,solve_algo=2)e gammaD ii	delta	taxD piC Y 	r b  ;

//FOR MER
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii,delta),irf=0,solve_algo=2)  piC Y e ii	delta d gammaD  taxDCol taxD  ;

//FOR FER
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii),irf=0,solve_algo=2)e gammaD ii	delta	taxD piC Y 	r b  ;

//FOR PER
//ramsey_policy(planner_discount=0.99,order=1,instruments=(delta),irf=0,solve_algo=2)e gammaD ii	delta	taxD piC Y 	r b  ;

//FOR CCR
//ramsey_policy(planner_discount=0.99,order=1,instruments=(taxD),irf=0,solve_algo=2)e gammaD ii	delta	taxD piC Y 	r b  ;

